﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class programClassDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="programClassId">标识列ID</param>
		public bool Exists(int programClassId)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [programClass] where programClassId=@programclassid ");
			htSyn.Add("@programclassid", programClassId);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<programClass> GetModel()
		{
			strSql.Remove(0, strSql.Length);
            strSql.Append("Select [programClassId],[isSort],[fID],[programClassName],[programTag],[programTitle],[programDescription],[programKey] FROM [programClass] order by [isSort] asc");
			return getprogramClassBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="programClassId">标识列ID</param>
		/// <returns></returns>
		public programClass GetModel(int programclassid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
            strSql.Append("select [programClassId],[isSort],[fID],[programClassName],[programTag],[programTitle],[programDescription],[programKey] FROM [programClass]");
			strSql.Append(" where programClassId=@programclassid ");
			htSyn.Add("@programclassid", programclassid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			programClass programclassModel = new programClass();
			if (dt.Rows.Count > 0)
			{
				programclassModel.isSort=Convert.ToInt32(dt.Rows[0]["isSort"]);
				programclassModel.fID=Convert.ToInt32(dt.Rows[0]["fID"]);
				programclassModel.programClassName=dt.Rows[0]["programClassName"].ToString();
				programclassModel.programTag=dt.Rows[0]["programTag"].ToString();
                programclassModel.programKey = dt.Rows[0]["programKey"].ToString();
                programclassModel.programTitle = dt.Rows[0]["programTitle"].ToString();
                programclassModel.programDescription = dt.Rows[0]["programDescription"].ToString();
			}
			return programclassModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="programClass">实体</param>
		/// <returns></returns>
		public bool Update(programClass programclassModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [programClass] set ");
			strSql.Append("[isSort]=@issort,");
			strSql.Append("[fID]=@fid,");
			strSql.Append("[programClassName]=@programclassname,");
            strSql.Append("[programKey]=@programkey,");
            strSql.Append("[programDescription]=@programdescription,");
            strSql.Append("[programTitle]=@programtitle,");
			strSql.Append("[programTag]=@programtag");
			strSql.Append(" where programClassId=@programclassid ");
			htSyn.Add("@programclassid", programclassModel.programClassId);
			htSyn.Add("@issort", programclassModel.isSort);
			htSyn.Add("@fid", programclassModel.fID);
			htSyn.Add("@programclassname", programclassModel.programClassName);
            htSyn.Add("@programkey", programclassModel.programKey);
			htSyn.Add("@programtag", programclassModel.programTag);
            htSyn.Add("@programtitle", programclassModel.programTitle);
            htSyn.Add("@programdescription", programclassModel.programDescription);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="programClassId">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int programClassId)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [programClass] where programClassId=@programclassid");
			htSyn.Add("programclassid", programClassId);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="programClass">实体</param>
		/// <returns></returns>
		public bool Insert(programClass programclassModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
            strSql.Append("insert into [programClass]([isSort],[fID],[programClassName],[programTag],[programKey],[programDescription],[programTitle]) values (@issort,@fid,@programclassname,@programtag,@programkey,@programdescription,@programtitle)");
			htSyn.Add("@issort", programclassModel.isSort);
			htSyn.Add("@fid", programclassModel.fID);

			htSyn.Add("@programclassname", programclassModel.programClassName);
            htSyn.Add("@programkey", programclassModel.programKey);
            htSyn.Add("@programdescription", programclassModel.programDescription);
			htSyn.Add("@programtag", programclassModel.programTag);
            htSyn.Add("@programtitle", programclassModel.programTitle);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<programClass> getprogramClassBySql(string strSql)
		{
			IList<programClass> list = new List<programClass>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					programClass programclassModel = new programClass();
					programclassModel.programClassId= Convert.ToInt32(dr["programClassId"]);
					programclassModel.isSort= Convert.ToInt32(dr["isSort"]);
					programclassModel.fID= Convert.ToInt32(dr["fID"]);
					programclassModel.programClassName=dr["programClassName"].ToString();

                    programclassModel.programKey = dr["programKey"].ToString();
                    programclassModel.programTitle = dr["programTitle"].ToString();
                    programclassModel.programDescription = dr["programDescription"].ToString();

					programclassModel.programTag=dr["programTag"].ToString();
					list.Add(programclassModel);
				}
			}
			return list;
		}
		#endregion

        #region 获得DataTable
        /// <summary>
        /// 获得DataTable
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable(int pcid)
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("Select [programClassId],[isSort],[fID],[programClassName],[programTag],[programKey],[programDescription],[programTitle] FROM [programClass]  ");
            if (pcid!=0)
            {
                strSql.Append(" where programClassId=@programclassid");
                htSyn.Add("@programclassid",pcid);
            }
            strSql.Append(" order by [isSort] asc");
            return db.GetDataTable(strSql.ToString(),htSyn);          
        }
        #endregion

        #region 根据父id获得信息
        /// <summary>
        /// 根据父id获得信息
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="fid"></param>
        /// <returns></returns>
        public DataView GetDataView(DataTable dt, int fid) 
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[fID]="+fid;
            return dv;
        }
        #endregion

        #region 根据类别id，获得类名称
        /// <summary>
        /// 根据类别id，获得类名称
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="pcid"></param>
        /// <returns></returns>
        public DataView GetModel(DataTable dt, int pcid) 
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[programClassId]="+pcid;
            return dv;
        }
        #endregion

        #region 是否存在子类
        /// <summary>
        /// 是否存在子类
        /// </summary>
        /// <param name="programClassId"></param>
        /// <returns></returns>
        public bool ExistsChildren(string programClassId) 
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from [programClass] where [fID]=@fid");
            htSyn.Add("@fid",programClassId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="programClassIds"></param>
        /// <returns></returns>
        public bool delete(string[] programClassIds)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from programClass where");
            foreach (string pcid in programClassIds)
            {
                strSql.AppendFormat(" programClassId={0} or", pcid);
            }
            strSql = strSql.Remove(strSql.Length - 1, 1);
            strSql = strSql.Remove(strSql.Length - 1, 1);
            if (db.ExecuteNonQuery(strSql.ToString()) > 0)
            {
                return true;
            }
            return false; 
        }
        #endregion

        #region 批量移动
        /// <summary>
        /// 批量移动
        /// </summary>
        /// <param name="id"></param>
        /// <param name="fid"></param>
        /// <returns></returns>
        public bool Update(string[] pids,string fid)
        {
            strSql.Remove(0, strSql.Length);
            strSql.Append("update programClass set");
            strSql.AppendFormat(" fID={0}", fid);
            strSql = strSql.Append(" where ");
            foreach (string pcid in pids)
            {
                strSql.AppendFormat(" programClassId={0} or", pcid);
            }
            strSql = strSql.Remove(strSql.Length - 1, 1);
            strSql = strSql.Remove(strSql.Length - 1, 1);
            if (db.ExecuteNonQuery(strSql.ToString()) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion
    }
}
